package com.example.myapplication.uitls

import com.tencent.mmkv.MMKV

/***保存本地数据*/
class MMKVUtils {
    companion object {
        const val MMKV_ID = "http_cache_id"

        lateinit var mmkv: MMKV
        private fun getMMKV(): MMKV {
            if (this::mmkv.isInitialized)
                mmkv = MMKV.mmkvWithID(MMKV_ID)
            return mmkv
        }

        fun putString(key: String, value: String = "") {
            getMMKV().putString(key, value).commit()
        }

        fun put(key: String, value: String = "") {
            putString(key, value)
        }

        fun getString(key: String, value: String = ""): String? {
            return getMMKV().getString(key, value);
        }

        fun putFloat(key: String, value: Float) {
            getMMKV().putFloat(key, value).commit()
        }

        fun put(key: String, value: Float) {
            putFloat(key, value)
        }

        fun getFloat(key: String, value: Float): Float {
            return getMMKV().getFloat(key, value);
        }

        fun putInt(key: String, value: Int = 0) {
            getMMKV().putInt(key, value).commit()
        }

        fun put(key: String, value: Int = 0) {
            putInt(key, value)
        }

        fun getInt(key: String, value: Int = 0): Int {
            return getMMKV().getInt(key, value);
        }

        fun putLong(key: String, value: Long = 0L) {
            getMMKV().putLong(key, value).commit()
        }

        fun put(key: String, value: Long = 0L) {
            putLong(key, value)
        }

        fun getLong(key: String, value: Long = 0L): Long {
            return getMMKV().getLong(key, value);
        }

        fun putBoolean(key: String, value: Boolean = false) {
            getMMKV().putBoolean(key, value).commit()
        }

        fun put(key: String, value: Boolean = false) {
            putBoolean(key, value)
        }

        fun getBoolean(key: String, value: Boolean = false): Boolean {
            return getMMKV().getBoolean(key, value);
        }
    }
}